package com.cd.university.config.security.validate.email;

import com.cd.university.config.security.properties.SecurityProperties;
import com.cd.university.config.security.validate.ValidateCode;
import com.cd.university.config.security.validate.ValidateCodeGenerator;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * 邮件验证码的生成器
 * @author 陈栋
 * @create 2021/9/10 13:42
 */
@Component("emailValidateCodeGenerator")
public class EmailValidateCodeGenerator implements ValidateCodeGenerator {

    @Autowired
    private SecurityProperties securityProperties;

    @Override
    public ValidateCode generate(ServletWebRequest request) {
        // 读取邮件配置中的长度，生成指定长度的验证码
        String emailCode = RandomStringUtils.randomNumeric(securityProperties.getCode().getEmailCode().getLength());
        // 默认设置为10分钟
        int expireIn = securityProperties.getCode().getEmailCode().getExpireIn();
        return new ValidateCode(emailCode, expireIn);
    }
}
